﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorkStation.UI.Controls
{
    public class MotionDirectRectangle
    {
        private List<MotionShape> _shapes;
        private RectangleMotion _directRectLeft;
        private LineMotion _directLineLeft;
        private RectangleMotion _directRectRight;
        private LineMotion _directLineRight;
        private RectangleMotion _directRectUp;
        private LineMotion _directLineUp;
        private RectangleMotion _directRectDown;
        private LineMotion _directLineDown;
        public string Description { get; set; }

        public MotionDirectRectangle(string description, List<MotionShape> shapes)
        {
            this.Description = description;
            this._shapes = shapes;
        }
        public void Update(MotionCoordinate xCrd, MotionCoordinate yCrd, MotionCoordinate rCrd, int totalWidth, int totalHeight, int marginLeft, int marginUp, bool isSelectedLeft = false,
                                                                                                                                                               bool isSelectedRight = false,
                                                                                                                                                               bool isSelectedUp = false,
                                                                                                                                                               bool isSelectedDown = false)
        {
            _shapes.RemoveAll(s => s.Description == $"{Description}_LeftRect");
            _shapes.RemoveAll(s => s.Description == $"{Description}_RightRect");
            _shapes.RemoveAll(s => s.Description == $"{Description}_UpRect");
            _shapes.RemoveAll(s => s.Description == $"{Description}_DownRect");
            _shapes.RemoveAll(s => s.Description == $"{Description}_LeftLine");
            _shapes.RemoveAll(s => s.Description == $"{Description}_RightLine");
            _shapes.RemoveAll(s => s.Description == $"{Description}_UpLine");
            _shapes.RemoveAll(s => s.Description == $"{Description}_DownLine");
            if (xCrd != null && yCrd != null)
            {
                _directRectLeft = new RectangleMotion($"{Description}_LeftRect");
                _directRectLeft.Alpha = 60;
                _directRectLeft.LineWidth = 1;
                _directRectLeft.PaintColor = isSelectedLeft ? Color.Blue : Color.DeepSkyBlue;
                _directRectLeft.FillColor = isSelectedLeft ? Color.Blue : Color.DeepSkyBlue;
                _directRectLeft.IsFill = true;
                _shapes.Add(_directRectLeft);
                _directRectLeft.StartPoint = new PointF((float)marginLeft + 20 - 15, (float)(marginUp + totalHeight / 2 - 15));
                _directRectLeft.EndPoint = new PointF((float)marginLeft + 20 + 15, (float)(marginUp + totalHeight / 2 + 15));

                _directLineLeft = new LineMotion($"{Description}_LeftLine");
                _directLineLeft.PaintColor = Color.Yellow;
                _directLineLeft.LineWidth = 3;
                _directLineLeft.StartPoint = new PointF((float)marginLeft + 20 + 20, (float)(marginUp + totalHeight / 2));
                _directLineLeft.EndPoint = new PointF((float)marginLeft + 20 - 20, (float)(marginUp + totalHeight / 2));
                _directLineLeft.EnableEndCap = true;    
                _shapes.Add(_directLineLeft);

                _directRectRight = new RectangleMotion($"{Description}_RightRect");
                _directRectRight.Alpha = 60;
                _directRectRight.LineWidth = 1;
                _directRectRight.PaintColor = isSelectedRight ? Color.Blue : Color.DeepSkyBlue;
                _directRectRight.FillColor = isSelectedRight ? Color.Blue : Color.DeepSkyBlue;
                _directRectRight.IsFill = true;
                _shapes.Add(_directRectRight);
                _directRectRight.StartPoint = new PointF((float)(marginLeft + totalWidth - 20 - 15), (float)(marginUp + totalHeight / 2 - 15));
                _directRectRight.EndPoint = new PointF((float)(marginLeft + totalWidth - 20 + 15), (float)(marginUp + totalHeight / 2 + 15));

                _directLineRight = new LineMotion($"{Description}_RightLine");
                _directLineRight.PaintColor = Color.Yellow;
                _directLineRight.LineWidth = 3;
                _directLineRight.StartPoint = new PointF((float)(marginLeft + totalWidth - 20 - 20), (float)(marginUp + totalHeight / 2));
                _directLineRight.EndPoint = new PointF((float)(marginLeft + totalWidth - 20 + 20), (float)(marginUp + totalHeight / 2));
                _directLineRight.EnableEndCap = true;
                _shapes.Add(_directLineRight);

                _directRectUp = new RectangleMotion($"{Description}_UpRect");
                _directRectUp.Alpha = 60;
                _directRectUp.LineWidth = 1;
                _directRectUp.PaintColor = isSelectedUp ? Color.Blue : Color.DeepSkyBlue;
                _directRectUp.FillColor = isSelectedUp ? Color.Blue : Color.DeepSkyBlue;
                _directRectUp.IsFill = true;
                _shapes.Add(_directRectUp);
                _directRectUp.StartPoint = new PointF((float)(marginLeft + totalWidth / 2 - 15), (float)(marginUp + 20 - 15));
                _directRectUp.EndPoint = new PointF((float)(marginLeft + totalWidth / 2 + 15), (float)(marginUp + 20 + 15));

                _directLineUp = new LineMotion($"{Description}_UpLine");
                _directLineUp.PaintColor = Color.Yellow;
                _directLineUp.LineWidth = 3;
                _directLineUp.StartPoint = new PointF((float)(marginLeft + totalWidth / 2), (float)(marginUp + 20 + 20));
                _directLineUp.EndPoint = new PointF((float)(marginLeft + totalWidth / 2), (float)(marginUp + 20 - 20));
                _directLineUp.EnableEndCap = true;
                _shapes.Add(_directLineUp);

                _directRectDown = new RectangleMotion($"{Description}_DownRect");
                _directRectDown.Alpha = 60;
                _directRectDown.LineWidth = 1;
                _directRectDown.PaintColor = isSelectedDown ? Color.Blue : Color.DeepSkyBlue;
                _directRectDown.FillColor = isSelectedDown ? Color.Blue : Color.DeepSkyBlue;
                _directRectDown.IsFill = true;
                _shapes.Add(_directRectDown);
                _directRectDown.StartPoint = new PointF((float)(marginLeft + totalWidth / 2 - 15), (float)(marginUp + totalHeight - 20 - 15));
                _directRectDown.EndPoint = new PointF((float)(marginLeft + totalWidth / 2 + 15), (float)(marginUp + totalHeight - 20 + 15));

                _directLineDown = new LineMotion($"{Description}_DownLine");
                _directLineDown.PaintColor = Color.Yellow;
                _directLineDown.LineWidth = 3;
                _directLineDown.StartPoint = new PointF((float)(marginLeft + totalWidth / 2), (float)(marginUp + totalHeight - 20 - 20));
                _directLineDown.EndPoint = new PointF((float)(marginLeft + totalWidth / 2), (float)(marginUp + totalHeight - 20 + 20));
                _directLineDown.EnableEndCap = true;
                _shapes.Add(_directLineDown);
            }
            if(xCrd == null && yCrd != null)
            {
                _directRectUp = new RectangleMotion($"{Description}_UpRect");
                _directRectUp.Alpha = 60;
                _directRectUp.LineWidth = 1;
                _directRectUp.PaintColor = isSelectedUp ? Color.Blue : Color.DeepSkyBlue;
                _directRectUp.FillColor = isSelectedUp ? Color.Blue : Color.DeepSkyBlue;
                _directRectUp.IsFill = true;
                _shapes.Add(_directRectUp);
                _directRectUp.StartPoint = new PointF((float)(marginLeft + totalWidth / 2 - 15), (float)(marginUp + 20 - 15));
                _directRectUp.EndPoint = new PointF((float)(marginLeft + totalWidth / 2 + 15), (float)(marginUp + 20 + 15));

                _directLineUp = new LineMotion($"{Description}_UpLine");
                _directLineUp.PaintColor = Color.Yellow;
                _directLineUp.LineWidth = 3;
                _directLineUp.StartPoint = new PointF((float)(marginLeft + totalWidth / 2), (float)(marginUp + 20 + 20));
                _directLineUp.EndPoint = new PointF((float)(marginLeft + totalWidth / 2), (float)(marginUp + 20 - 20));
                _directLineUp.EnableEndCap = true;
                _shapes.Add(_directLineUp);

                _directRectDown = new RectangleMotion($"{Description}_DownRect");
                _directRectDown.Alpha = 60;
                _directRectDown.LineWidth = 1;
                _directRectDown.PaintColor = isSelectedDown ? Color.Blue : Color.DeepSkyBlue;
                _directRectDown.FillColor = isSelectedDown ? Color.Blue : Color.DeepSkyBlue;
                _directRectDown.IsFill = true;
                _shapes.Add(_directRectDown);
                _directRectDown.StartPoint = new PointF((float)(marginLeft + totalWidth / 2 - 15), (float)(marginUp + totalHeight - 20 - 15));
                _directRectDown.EndPoint = new PointF((float)(marginLeft + totalWidth / 2 + 15), (float)(marginUp + totalHeight - 20 + 15));

                _directLineDown = new LineMotion($"{Description}_DownLine");
                _directLineDown.PaintColor = Color.Yellow;
                _directLineDown.LineWidth = 3;
                _directLineDown.StartPoint = new PointF((float)(marginLeft + totalWidth / 2), (float)(marginUp + totalHeight - 20 - 20));
                _directLineDown.EndPoint = new PointF((float)(marginLeft + totalWidth / 2), (float)(marginUp + totalHeight - 20 + 20));
                _directLineDown.EnableEndCap = true;
                _shapes.Add(_directLineDown);
            }
            if(xCrd != null && yCrd == null)
            {
                _directRectLeft = new RectangleMotion($"{Description}_LeftRect");
                _directRectLeft.Alpha = 60;
                _directRectLeft.LineWidth = 1;
                _directRectLeft.PaintColor = isSelectedLeft ? Color.Blue : Color.DeepSkyBlue;
                _directRectLeft.FillColor = isSelectedLeft ? Color.Blue : Color.DeepSkyBlue;
                _directRectLeft.IsFill = true;
                _shapes.Add(_directRectLeft);
                _directRectLeft.StartPoint = new PointF((float)marginLeft + 20 - 15, (float)(marginUp + totalHeight / 2 - 15));
                _directRectLeft.EndPoint = new PointF((float)marginLeft + 20 + 15, (float)(marginUp + totalHeight / 2 + 15));

                _directLineLeft = new LineMotion($"{Description}_LeftLine");
                _directLineLeft.PaintColor = Color.Yellow;
                _directLineLeft.LineWidth = 3;
                _directLineLeft.StartPoint = new PointF((float)marginLeft + 20 + 20, (float)(marginUp + totalHeight / 2));
                _directLineLeft.EndPoint = new PointF((float)marginLeft + 20 - 20, (float)(marginUp + totalHeight / 2));
                _directLineLeft.EnableEndCap = true;
                _shapes.Add(_directLineLeft);

                _directRectRight = new RectangleMotion($"{Description}_RightRect");
                _directRectRight.Alpha = 60;
                _directRectRight.LineWidth = 1;
                _directRectRight.PaintColor = isSelectedRight ? Color.Blue : Color.DeepSkyBlue;
                _directRectRight.FillColor = isSelectedRight ? Color.Blue : Color.DeepSkyBlue;
                _directRectRight.IsFill = true;
                _shapes.Add(_directRectRight);
                _directRectRight.StartPoint = new PointF((float)(marginLeft + totalWidth - 20 - 15), (float)(marginUp + totalHeight / 2 - 15));
                _directRectRight.EndPoint = new PointF((float)(marginLeft + totalWidth - 20 + 15), (float)(marginUp + totalHeight / 2 + 15));

                _directLineRight = new LineMotion($"{Description}_RightLine");
                _directLineRight.PaintColor = Color.Yellow;
                _directLineRight.LineWidth = 3;
                _directLineRight.StartPoint = new PointF((float)(marginLeft + totalWidth - 20 - 20), (float)(marginUp + totalHeight / 2));
                _directLineRight.EndPoint = new PointF((float)(marginLeft + totalWidth - 20 + 20), (float)(marginUp + totalHeight / 2));
                _directLineRight.EnableEndCap = true;
                _shapes.Add(_directLineRight);
            }
            if(rCrd != null)
            {
                _directRectLeft = new RectangleMotion($"{Description}_LeftRect");
                _directRectLeft.Alpha = 60;
                _directRectLeft.LineWidth = 1;
                _directRectLeft.PaintColor = isSelectedLeft ? Color.Blue : Color.DeepSkyBlue;
                _directRectLeft.FillColor = isSelectedLeft ? Color.Blue : Color.DeepSkyBlue;
                _directRectLeft.IsFill = true;
                _shapes.Add(_directRectLeft);
                _directRectLeft.StartPoint = new PointF((float)marginLeft + 20 - 15, (float)(marginUp + totalHeight / 2 - 15));
                _directRectLeft.EndPoint = new PointF((float)marginLeft + 20 + 15, (float)(marginUp + totalHeight / 2 + 15));

                _directLineLeft = new LineMotion($"{Description}_LeftLine");
                _directLineLeft.PaintColor = Color.Yellow;
                _directLineLeft.LineWidth = 3;
                _directLineLeft.StartPoint = new PointF((float)marginLeft + 20, (float)(marginUp + totalHeight / 2 - 20));
                _directLineLeft.EndPoint = new PointF((float)marginLeft + 20, (float)(marginUp + totalHeight / 2 + 20));
                _directLineLeft.EnableEndCap = true;
                _shapes.Add(_directLineLeft);

                _directRectRight = new RectangleMotion($"{Description}_RightRect");
                _directRectRight.Alpha = 60;
                _directRectRight.LineWidth = 1;
                _directRectRight.PaintColor = isSelectedRight ? Color.Blue : Color.DeepSkyBlue;
                _directRectRight.FillColor = isSelectedRight ? Color.Blue : Color.DeepSkyBlue;
                _directRectRight.IsFill = true;
                _shapes.Add(_directRectRight);
                _directRectRight.StartPoint = new PointF((float)(marginLeft + totalWidth - 20 - 15), (float)(marginUp + totalHeight / 2 - 15));
                _directRectRight.EndPoint = new PointF((float)(marginLeft + totalWidth - 20 + 15), (float)(marginUp + totalHeight / 2 + 15));

                _directLineRight = new LineMotion($"{Description}_RightLine");
                _directLineRight.PaintColor = Color.Yellow;
                _directLineRight.LineWidth = 3;
                _directLineRight.StartPoint = new PointF((float)(marginLeft + totalWidth - 20), (float)(marginUp + totalHeight / 2 - 20));
                _directLineRight.EndPoint = new PointF((float)(marginLeft + totalWidth - 20), (float)(marginUp + totalHeight / 2 + 20));
                _directLineRight.EnableEndCap = true;
                _shapes.Add(_directLineRight);
            }
        }

        public RectDirection PointOnDirectionRect(MotionCoordinate xCrd, MotionCoordinate yCrd, MotionCoordinate rCrd, int x, int y)
        {
            //if(xCrd != null && yCrd != null)
            {
                if(pointInRect(x, y, _directRectLeft))
                {
                    return RectDirection.Left;
                }
                else if (pointInRect(x, y, _directRectRight))
                {
                    return RectDirection.Right;
                }
                else if (pointInRect(x, y, _directRectUp))
                {
                    return RectDirection.Up;
                }
                else if (pointInRect(x, y, _directRectDown))
                {
                    return RectDirection.Down;
                }
            }
            return RectDirection.Out;
        }
        
        private bool pointInRect(int x, int y, RectangleMotion rect)
        {
            if (rect == null)
                return false;
            return (x >= rect.StartPoint.X && x <= rect.EndPoint.X && y >= rect.StartPoint.Y && y <= rect.EndPoint.Y);
        }
    }
    public enum RectDirection
    {
        Out,
        Up,
        Down,
        Left,
        Right,
    }
}
